package org.vsc.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import sun.security.provider.SHA;
import sun.security.rsa.RSASignature.SHA1withRSA;
import sun.security.util.ObjectIdentifier;

import com.sun.org.apache.bcel.internal.generic.CPInstruction;
import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm;

public class test {
	public static void main(String[] argv) {
		String salt = "d8a47ebaaede9ae5470d27d57835711723d0d8cd";
		String key = "f9287b158c228640dab382580b88b528b23dc5da";
		int times = 10;
		
		try {
			System.out.println(encrypt("liuliang", salt, times, key));
			
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	public static String encrypt(String password, String salt, int times, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
		String digest = key;
		for (int i = 0; i < times; i++) {
			digest = sha1(digest, salt, password, key);
		}
		
		return digest;
	}
	
	public static String sha1(String digest, String salt, String password, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
		String msg = digest + "--" + salt + "--" + password + "--" + key;
		
		MessageDigest md = null;
		md = MessageDigest.getInstance("SHA-1");
		return toEncryptedString(md.digest(msg.getBytes("UTF-8")));
	}
	
	public static String toEncryptedString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			String hex = String.format("%02x", b);
			sb.append(hex);
		}
		return sb.toString();
	}
}
